package com.administrator.customviewtest.ipc.messager;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by lsp on 2017/9/22 10 38
 * 服务端
 */

public class MessagerService extends Service {
    private static class MessagerHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Log.e("ssss",msg.getData().getString("msg"));
//                    Toast.makeText(MessagerService.this,msg.getData().getString("msg"),Toast.LENGTH_SHORT).show();
                    Messenger client =  msg.replyTo;
                    Message message = Message.obtain(null,1);
                    Bundle bundle = new Bundle();
                    bundle.putString("data","呵呵呵");
                    message.setData(bundle);
                    try {
                        client.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    }
    private Messenger messenger = new Messenger(new MessagerHandler());
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}
